using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Configuration.Install;
using MySql.Data.MySqlClient;

namespace dbInstall
{
    [RunInstaller(true)]
    public class CSharpDeployInstaller: Installer
    {
        private MySqlConnection masterConnection = new MySqlConnection();
        public CSharpDeployInstaller()
            : base()
        {
        }

        private string GetSql(string Name)
        {
            try
            {
                // Gets the current assembly.
                Assembly Asm = Assembly.GetExecutingAssembly();

                // Resources are named using a fully qualified name.
                Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);

                // Reads the contents of the embedded file.
                StreamReader reader = new StreamReader(strm);
                return reader.ReadToEnd();

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        protected void InstallDB(string strDBName, string server, string dbType, string user, string pass)
        {
            MySqlCommand Database = new MySqlCommand("Create Database " + strDBName, masterConnection);
            MySqlCommand Tables = new MySqlCommand(GetSql("sqlFile.txt"), masterConnection);
            try
            {
                Database.Connection.ConnectionString = "Server=" + server + ";Database=mysql;Uid=" + user + ";Pwd=" + pass + ";";
                Database.Connection.Open();

                Database.ExecuteNonQuery();

                Tables.Connection.ChangeDatabase(strDBName);

                Tables.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                // Reports any errors and abort.
                throw ex;
            }
            finally
            {
                Database.Connection.Close();
            }
        }

        /*protected void DBModuleInstall(bool pricing, bool batch, bool grocery, bool recipe, string strDBName, string user, string pass, string server)
        {
            if (pricing)
            {
                MySqlCommand Database = new MySqlCommand("Create Database " + strDBName, masterConnection);
                MySqlCommand Tables = new MySqlCommand(GetSql("module-pricing.txt"), masterConnection);

                try
                {
                    Database.Connection.ConnectionString = "Server=" + server + ";Database=mysql;Uid=" + user + ";Pwd=" + pass + ";";
                    Database.Connection.Open();

                    Tables.Connection.ChangeDatabase(strDBName);
                    Tables.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    // Reports any errors and abort.
                    throw ex;
                }
                finally
                {
                    Database.Connection.Close();
                }
            } 
            
            if (batch)
            {
                MySqlCommand Database = new MySqlCommand("Create Database " + strDBName, masterConnection);
                MySqlCommand Tables = new MySqlCommand(GetSql("module-batch.txt"), masterConnection);

                try
                {
                    Database.Connection.ConnectionString = "Server=" + server + ";Database=mysql;Uid=" + user + ";Pwd=" + pass + ";";
                    Database.Connection.Open();

                    Tables.Connection.ChangeDatabase(strDBName);
                    Tables.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    // Reports any errors and abort.
                    throw ex;
                }
                finally
                {
                    Database.Connection.Close();
                }
            }
            
            if (grocery)
            {
                MySqlCommand Database = new MySqlCommand("Create Database " + strDBName, masterConnection);
                MySqlCommand Tables = new MySqlCommand(GetSql("module-grocery.txt"), masterConnection);

                try
                {
                    Database.Connection.ConnectionString = "Server=" + server + ";Database=mysql;Uid=" + user + ";Pwd=" + pass + ";";
                    Database.Connection.Open();

                    Tables.Connection.ChangeDatabase(strDBName);
                    Tables.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    // Reports any errors and abort.
                    throw ex;
                }
                finally
                {
                    Database.Connection.Close();
                }
            } 
            
            if (recipe)
            {
                MySqlCommand Database = new MySqlCommand("Create Database " + strDBName, masterConnection);
                MySqlCommand Tables = new MySqlCommand(GetSql("module-recipe.txt"), masterConnection);

                try
                {
                    Database.Connection.ConnectionString = "Server=" + server + ";Database=mysql;Uid=" + user + ";Pwd=" + pass + ";";
                    Database.Connection.Open();

                    Tables.Connection.ChangeDatabase(strDBName);
                    Tables.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    // Reports any errors and abort.
                    throw ex;
                }
                finally
                {
                    Database.Connection.Close();
                }
            }
        }*/

        public override void Install(IDictionary savedState)
        {
            base.Install(savedState);
            InstallDB(this.Context.Parameters["dbname"], this.Context.Parameters["hostname"], this.Context.Parameters["dbType"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"]);
            //DBModuleInstall(Convert.ToBoolean(this.Context.Parameters["pricing"]), Convert.ToBoolean(this.Context.Parameters["pricing"]), Convert.ToBoolean(this.Context.Parameters["pricing"]), Convert.ToBoolean(this.Context.Parameters["pricing"]), this.Context.Parameters["dbname"], this.Context.Parameters["uid"], this.Context.Parameters["pwd"], this.Context.Parameters["hostname"]);
        }

    }
}
